/**
 * Created by HY on 2014/12/18 0018.
 */
$(function () {
	textarea_init();
	share_bind();
	process_bind();
});

/************************
 *
 * bind methdos
 *
 * ***********************/
function share_bind() {
	$("#share").on("click", share);
}

function process_bind() {
	$(".main_left").on("click", ".process", function () {
		process(this);
	})
}


/************************
 *
 * common methods
 *
 * ***********************/

/**
 *  防止firefox使用F5刷新导致textarea被赋值
 */
function textarea_init() {
	$("#note").val("");
	$("#note").attr("placeholder", "who are you?");
	textarea_disable();
}

function textarea_disable() {
	$("#note").attr("disabled", "disabled");
}

function get_chose_code(ls_id) {
	return $("#" + ls_id).find(".process_chosen").attr("code");
}


/************************
 *
 * action methdos
 *
 * ***********************/


/**
 * 发布分享
 */
function share() {
	var content = getVal("note");
	var role_code = get_chose_code("role_ls");
	var location_code = get_chose_code("location_ls");
	var incident_code = get_chose_code('incident_ls');
	var custom_role = getVal("custom_role");
	var custom_location = getVal("custom_location");
	var custom_incident = getVal("custom_incident");
//	if (!notEmpty(role_code) || !notEmpty(location_code) || !notEmpty(incident_code) || !notEmpty(content)) {
//		alert("参数错误");
//		return;
//	}
	setVal("content", content);
	setVal("role", role_code);
	setVal("location", location_code);
	setVal("incident", incident_code);
	setVal("roleName", custom_role);
	setVal("locationName", custom_location);
	setVal("incidentName", custom_incident);
	ajaxSubmitJson("share_action", null, defaultResponse);
}

var tag;

/**
 * 左端每一次的点击事件
 * @param span_obj
 */
function process(span_obj) {
	var href = $(span_obj).find("a");
	tag = $(href).attr("tag");
	var code = $(href).attr("code");
	var text = $(href).html();

	var role_code = get_chose_code("role_ls");
	var location_code = get_chose_code("location_ls");
	var incident_code = null;

	function role_chose_operation() {
		var old_span = $("#role_ls").find(".process_chosen");
		var old_text = $(old_span).html();
		var old_code = $(old_span).attr('code');
		var old_insert_href = '<a href="javascript:;" tag="role" code="' + old_code + '">' + old_text + '<\/a>';
		$(old_span).html(old_insert_href).attr("class", "process");
		role_code = code;
		location_code = null;
		$("#location_ls").html("");
		$("#incident_ls").html("");
		textarea_disable();
	}

	function location_chose_operation() {
		var old_span = $("#location_ls").find(".process_chosen");
		var old_text = $(old_span).html();
		var old_code = $(old_span).attr('code');
		var old_insert_href = '<a href="javascript:;" tag="location" code="' + old_code + '">' + old_text + '<\/a>';
		$(old_span).html(old_insert_href).attr("class", "process");
		location_code = code;
		$("#incident_ls").html("");
		textarea_disable();
	}

	function incident_chose_operation() {
		var old_span = $("#incident_ls").find(".process_chosen");
		var old_text = $(old_span).html();
		var old_code = $(old_span).attr('code');
		var old_insert_href = '<a href="javascript:;" tag="incident" code="' + old_code + '">' + old_text + '<\/a>';
		$(old_span).html(old_insert_href).attr("class", "process");
		incident_code = code;

		/**
		 * incident目前只改变页面元素,没比较再次ajax提交
		 * 把改变点击元素的事件提前到这里,供操作完之后return
		 */
		$(span_obj).attr("class", "process_chosen").html(text).attr("code", code);
		$("#note").attr("placeholder", "想分享些什么呢?");
		$("#note").attr("disabled", false);
	}

	if (tag == "role") {
		role_chose_operation();
	} else if (tag == "location") {
		location_chose_operation();
	} else if (tag == "incident") {
		incident_chose_operation();
		return;
	}

	$(span_obj).attr("class", "process_chosen").html(text).attr("code", code);

	var params = {"role": role_code, "location": location_code, "incident": incident_code};
	ajaxRequestGetHtml(location.href, params, response);
}

function response(html) {
	if (tag == "role") {
		textarea_init();
		$("#location_ls").html(html);
		$("#note").attr("placeholder", "where are you?")
	} else if (tag == "location") {
		textarea_init();
		$("#incident_ls").html(html);
		$("#note").attr("placeholder", "what are you doing?");
	}
}


